<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&family=Pacifico&display=swap"
          rel="stylesheet">

    <title>动态祝福语打字效果</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: gray;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        .box {
            width: 100vw;
            transform-style: preserve-3d;
            transition: transform 1s;
            transform-origin: center center; /* 关键修改：设置中线为旋转轴 */
        }

        .box.flipped {
            transform: rotateX(360deg);
        }

        .box.flipped1 {
            transform: rotateY(360deg);
        }

        .box.flipped2 {
            transform: rotateZ(360deg);
        }

        .greeting-card-front, .greeting-card-back {
            position: absolute;
            backface-visibility: hidden;
            background-color: black;
            /*background-image: url("img/bg.png") ;*/
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            padding: 30px;
            min-height: 400px;
            width: 100%;
            text-align: center;
            box-sizing: border-box;
        }

        .content {
            text-align: left;
            font-size: 2rem;
            min-height: 3rem;
            color: #ffffff;
            font-weight: bold;
            overflow: hidden;
            font-family: 'Pacifico', cursive;
            text-indent: 2em;
            white-space: break-spaces;
        }

        .simulated-round {
            text-align: left;
            width: 100%;
            font-family: 'Pacifico', cursive;
            font-size: 3rem;
            color: #ffffff;
            text-shadow: 1px 1px 0px #8B4513,
            2px 2px 3px rgba(139, 69, 19, 0.3);
            display: inline-block;
        }

        .greeting-card-back {
            background-color: #e74c3c;
            color: white;
            transform: rotateY(180deg);
        }

    </style>
    <style>
        .slide-in-container {
            width: 100%;
            position: relative;
        }

        .slide-in-box {
            position: absolute;
            right: -250px;
            opacity: 0;
            /* 动画配置 */
            animation: slideInFromRight 1s ease-out forwards;
        }

        /* 延迟动画 */
        .delay-1 {
            animation-delay: 0.5s;
        }

        .delay-2 {
            animation-delay: 1s;
        }

        @keyframes slideInFromRight {
            from {
                right: -250px;
                opacity: 0;
            }
            to {
                right: 0;
                opacity: 1;
            }
        }

        /* 带弹跳效果的变体 */
        .bounce-in-box {
            animation: slideInWithBounce 1.2s cubic-bezier(0.68, -0.55, 0.265, 1.55) forwards;
        }

        @keyframes slideInWithBounce {
            0% {
                right: -250px;
                opacity: 0;
            }

            25% {
                right: 100px;
                opacity: 1;
            }

            50% {
                right: -100px;
                opacity: 1;
            }

            70% {
                right: 100px;
                opacity: 1;
            }

            85% {
                right: -100px;
                opacity: 1;
            }
            100% {
                right: 0;
                opacity: 1;
            }
        }
    </style>
</head>
<body>
<div class="box greeting-card-front slide-in-box bounce-in-box delay-2" id="flipper">
    <div class="simulated-round"><span id="auther"></span></div>
    <div class="content" id="typewriter">
    </div>
    <img id="left_top1" src="./img/left_top1.jpg" style="position: absolute;top: 0;left: 0;width: 140px;">
    <div id="redDot" style="background-color: red;
    width: 14px;
    height: 14px;
    border-radius: 10px;
    position: absolute;
    top: 22px;
    left: 73px;"></div>
    <img src="./img/right_top.jpg" style="position: absolute;top: 0;right: 0;width: 140px;">


    <img src="./img/right_bottom.jpg" style="position: absolute;bottom: 0;right: 0;width: 140px;">
</div>
<!-- 音频元素 -->
<audio id="audioPlayer" preload="auto">
    <source src="./audio/keyboard.mp3" type="audio/mpeg">
</audio>
<script>

    setInterval(function () {
        if (document.getElementById("redDot").style.display == 'none') {
            document.getElementById("redDot").style.display = 'block';
        } else {
            document.getElementById("redDot").style.display = 'none';
        }
    }, 800)

    let timeOutItem = null;
    let timeOutItem1 = null;
    let timeOutItem2 = null;
    const audioPlayer = document.getElementById('audioPlayer');

    const greetings = [
        {
            text: '春风化雨润心田 , 岁月静好共光辉 。 愿你的每一天都充满阳光和欢笑 , 每一个梦想都能实现得淋漓尽致 。 祝福你前程似锦 , 万事如意 ! ',
            auther: "姚书记"
        },
        {
            text: "祝福如春雨 , 滋润心田间。愿你笑容常开 , 生活美满甜 ; 事业步步高 , 前程似锦绣。健康快乐伴你左右 , 幸福永远与你相伴!",
            auther: "习大大"
        },
        {
            text: "祝福如春风般温暖 , 如夏雨般滋润 , 如秋月般皎洁 , 如冬雪般纯净 。 愿你的每一天都充满阳光和欢笑 , 每一步都走向成功和幸福!",
            auther: "彭奶奶"
        },
        {
            text: "一份真挚的祝福 , 一份温暖的问候 , 愿你的生活充满阳光和温馨 , 愿你的每一天都过得充实而快乐。 祝福你 , 好运永伴!",
            auther: "叮当猫"
        },
        {
            text: "愿你笑口常开 , 福星高照 ; 愿你梦想成真 , 前程似锦 ; 愿你岁岁平安 , 年年有余。祝福你 , 吉祥如意 , 幸福安康!",
            auther: "联合国"
        }
    ];

    function writeTitle(text) {
        const element = document.getElementById('auther');
        let i = 0;
        element.innerHTML = '';

        function write() {
            if (i < text.length) {
                if (i % 2 == 0) {
                    element.innerHTML += text.charAt(i) + '|';
                } else {
                    element.innerHTML = element.innerHTML.slice(0, element.innerHTML.length - 1)
                    element.innerHTML += text.charAt(i);
                }
                i++;
                timeOutItem = setTimeout(write, 200);
                audioPlayer.play();
            } else {
                if (element.innerHTML.endsWith("|")) {
                    element.innerHTML = element.innerHTML.slice(0, element.innerHTML.length - 1)
                }
            }

        }

        write();

    }

    function typingEffect(elementId, text, speed) {
        const element = document.getElementById(elementId);
        let i = 0;
        element.innerHTML = '';

        function type() {
            if (i < text.length) {
                if (i % 2 == 0) {
                    element.innerHTML += text.charAt(i) + '|';
                } else {
                    element.innerHTML = element.innerHTML.slice(0, element.innerHTML.length - 1)
                    element.innerHTML += text.charAt(i);
                }
                i++;
                timeOutItem = setTimeout(type, speed);
            } else {
                if (element.innerHTML.endsWith("|")) {
                    element.innerHTML = element.innerHTML.slice(0, element.innerHTML.length - 1)
                }
                audioPlayer.pause();
                audioPlayer.currentTime = 0;
            }

        }

        type();

    }

    function showRandomGreeting() {
        document.getElementById("auther").innerHTML = '';
        setTimeout(function () {
            const randomIndex = Math.floor(Math.random() * greetings.length);
            const greeting = greetings[randomIndex];
            writeTitle(greeting.auther + ' , 祝 :')
            timeOutItem1 = setTimeout(function () {
                typingEffect('typewriter', greeting.text, 200 + Math.random() * 50);
            }, 2000)
        }, 1000)

    }

    function flipCard() {
        const flipper = document.getElementById('flipper');

        let hasClass = false;
        if (flipper.classList.contains('flipped')) {
            flipper.classList.remove('flipped');
            hasClass = true;
        }

        if (flipper.classList.contains('flipped1')) {
            flipper.classList.remove('flipped1');
            hasClass = true;
        }

        if (flipper.classList.contains('flipped2')) {
            flipper.classList.remove('flipped2');
            hasClass = true;
        }

        if (!hasClass) {
            let random = (Math.random() * 100).toFixed(0) % 3;
            if (random === 0) {
                flipper.classList.add('flipped');
            } else if (random === 1) {
                flipper.classList.add('flipped1');
            } else if (random === 2) {
                flipper.classList.add('flipped2');
            }
        }


        document.getElementById("typewriter").innerHTML = "";
        clearTimeout(timeOutItem);
        clearTimeout(timeOutItem1);
        audioPlayer.pause();
        audioPlayer.currentTime = 0;
        showRandomGreeting();
    }

    document.addEventListener('DOMContentLoaded', showRandomGreeting);
    document.body.addEventListener('click', flipCard);


</script>
</body>
</html>